package org.gordonframework.core.log {
	import flash.external.ExternalInterface;
	import flash.utils.describeType;	
	
	/**
	 * @author sampet
	 */
	public class GConsole {
		
		public static function debug(...args):void {
			trace.apply( null, args );
			if(ExternalInterface.available) {
				args.unshift( "console.log" );
				ExternalInterface.call.apply( null, args );
			}
		}

		public static function info(...args):void {
			trace.apply( null, args );
			if(ExternalInterface.available) {
				args.unshift( "console.info" );
				ExternalInterface.call.apply( null, args );
			}
		}

		public static function warn(...args):void {
			trace.apply( null, args );
			if(ExternalInterface.available) {
				args.unshift( "console.warn" );
				ExternalInterface.call.apply( null, args );
			}	
		}

		public static function error(...args):void {
			trace.apply( null, args );
			if(ExternalInterface.available) {
				args.unshift( "console.error" );
				ExternalInterface.call.apply( null, args );
			}
		}			

		public static function assert(expression:Boolean, ...args):void {
			if(!expression) {
				trace.apply( null, args );
				if(ExternalInterface.available) {
					args.unshift( "console.assert" );
					ExternalInterface.call.apply( null, args );
				}		
			}
		}

		public static function dir(object:Object):void {
			var description:XML = describeType( object );
			var list:XMLList = description.accessor + description.variable;
			for each(var node:XML in list) {
				trace( node.@name + ":" + object[node.@name ] );
			}
			for(var property:String in object) {
				trace( property + ":" + object[property] );
			}
			if(ExternalInterface.available) {
				try {
					ExternalInterface.call( "console.dir", object );
				}
				catch(e:Error) {
					error( e.message );
				}
			}
		}		
		
	}
}
